#ifndef __GPRS_H
#define __GPRS_H		
#include "stm32f10x.h"
#include "atcommand.h"
#include "gpio.h"
#include "wdg.h"


//当前固件版本号
#define CURRENT_FIRMWARE_VERSION 1
//当前协议版本号
#define CURRENT_PROTOCOL_VERSION 2
 
//H20消息头的长度
#define MSGH20_HEAD_LENGTH  20
//H20消息体缓冲区大小 390*2
#define MSGH20_BODY_BUFFER_SIZE 1200


//H4消息的头长度
#define MSGH4_HEAD_LENGTH 4
//H4消息缓冲区长度
#define MSGH4_BODY_BUFFER_SIZE 640




//循环队列
#define QUEUE_SIZE 1024


typedef struct  tagH20Message
{
    u8 FirmwareVersion; //dtu软件版本
	  u8 ProtocolVersion;  //协议版本
    unsigned char IMEI[15];
    u8  MessageType;
	  u16  BodyLength ;
    u8 HeadBodyData[MSGH20_HEAD_LENGTH+MSGH20_BODY_BUFFER_SIZE];

} H20Message;

typedef struct	tagH4Message
{
	  u8  Version;
		u16	BodyLength ;
		u8  MessageType;
		u8  HeadBodyData[MSGH4_HEAD_LENGTH+MSGH4_BODY_BUFFER_SIZE];

} H4Message;

enum MessageType
{
    MSG_DATA = 0x01,        //数据包
    MSG_HEARTBEAT = 0x02,//心跳包
    MSG_CONFIG = 0x03, //远程配置
	
    MSG_SENDRESULT = 0x04,
    MSG_STATUSRESULT = 0x05,
    MSG_LISTEN_DTU = 0x06,
    MSG_DCC_ID = 0x07,
    MSG_DISC_DTU = 0x08,
    MSG_ADD_DTU = 0x09,
    MSG_AT_CMD = 0x0A,
    MSG_REG = 0x0B,
    MSG_SESSION_CREATED = 0x0c,
    MSG_NODTU = 0x0d,
    MSG_DTU_OFFLINE = 0x0e,
    MSG_SESSIONCLOSED = 0x0F,
    MSG_SUCCESS = 0x10,
    MSG_FAIL = 0x11, 
	
    T_REQ_START     = 0x12,		//终端请求下载升级文件
    T_REQ_N_DATA		=	0x13,		//终端请求下载升级文件第N包
    T_REQ_END			  = 0x14,		//终端通知平台下载包全部接收完毕
    S_RES_INFO			=	0x15,		//平台回复终端请求下载升级文件的请求
    S_RES_N_DATA		=	0x16		//平台回复终端请求下载升级文件第N包的请求，并发送下载包:
};


typedef struct
{
    char Elements[QUEUE_SIZE];
    int front;
    int rear;
} CircSeqQueue;


 

char DeQueue(char * e);
void ParseH20MessageHead(void); 
void PackH20MessageHead(void); 
 
u16 GetCRC(u8 *data1, u8 len);
void BuildModbusMessage( u8 address, u8 type, u16 start, u16 registers, u8 *message);
int PrepareConnect(void);
void GprsProcess(u8 Res);
H20Message * SendH20MsgToServerSync(H20Message *,u16 timeout);
H4Message * SendH4MsgToServerSync(H4Message * pH4Message,u16 timeout);
void SendToServer(u8 * data,u16 datalength ,u16 timeout);
u8 CloseConnect(void);
int PrepareCommand(void); 
void ParseH4MessageHead(void);
void PackH4MSGHead(void);

void InitH4Message(void);
void InitH20Message(void);
 H4Message * GetH4MessageBuffer(void);
 
H20Message * GetH20MessageBuffer(void);
 

 
#endif			

